package com.jz.community.moduleshopping.refund.ui;

import android.graphics.Color;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.TimeLineDecoration;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.R2;
import com.jz.community.moduleshopping.orderDetail.bean.OrderDetailBean;
import com.jz.community.moduleshopping.refund.adapter.RefundDetailAdapter;
import com.jz.community.moduleshopping.refund.adapter.RefundDetailGoodsAdapter;
import com.jz.community.moduleshopping.refund.bean.RefundDetailBean;
import com.jz.community.moduleshopping.refund.task.RefundDetailTask;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.jz.community.basecomm.utils.TimeLineDecoration.BEGIN;
import static com.jz.community.basecomm.utils.TimeLineDecoration.NORMAL;

public class RefundDetailActivity extends BaseMvpActivity {


    @BindView(R2.id.tv_back)
    ImageView tvBack;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.commit_order_toolbar)
    Toolbar commitOrderToolbar;
    @BindView(R2.id.tv_refund_indicator_one)
    TextView tvRefundIndicatorOne;
    @BindView(R2.id.ll_refund_indicator_one)
    LinearLayout llRefundIndicatorOne;
    @BindView(R2.id.tv_refund_indicator_two)
    TextView tvRefundIndicatorTwo;
    @BindView(R2.id.ll_refund_indicator_two)
    LinearLayout llRefundIndicatorTwo;
    @BindView(R2.id.tv_refund_indicator_three)
    TextView tvRefundIndicatorThree;
    @BindView(R2.id.ll_refund_indicator_three)
    LinearLayout llRefundIndicatorThree;
    @BindView(R2.id.ll_refund_indicator)
    LinearLayout llRefundIndicator;
    @BindView(R2.id.rv_refund_detail_list)
    RecyclerView rvRefundDetailList;
    @BindView(R2.id.rv_refund_goods_list)
    RecyclerView rvRefundGoodsList;
    RefundDetailAdapter refundDetailAdapter;
    RefundDetailGoodsAdapter refundDetailGoodsAdapter;
    OrderDetailBean orderDetailBean;

    @Override
    protected int getContentView() {
        return R.layout.module_shopping_activity_refund_detail;
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this)
                .titleBar(commitOrderToolbar)
                .statusBarDarkFont(true, 0.2f)
                .init();
        tvTitle.setText("退款详情");
        orderDetailBean = (OrderDetailBean) getIntent().getSerializableExtra("orderDetailBean");

        TimeLineDecoration decoration = new TimeLineDecoration(this)
                .setLineColor(R.color.dividerColor)
                .setLineWidth(1)
                .setLeftDistance(25)
                .setTopDistance(0)
                .setNormalMarker(R.drawable.marker)
                .setBeginMarker(R.drawable.begin_marker)
                .setEndMarker(R.drawable.marker)
                .setCallback(new TimeLineDecoration.TimeLineCallback() {
                    @Override
                    public boolean isShowDivider(int position) {
                        return false;
                    }

                    @Override
                    public Rect getRect(int position) {
                        return null;
                    }

                    @Override
                    public int getTimeLineType(int position) {
                        if (position == 0) {
                            return BEGIN;
                        } else {
                            return NORMAL;
                        }
                    }
                });
        rvRefundDetailList.setLayoutManager(new LinearLayoutManager(this));
        refundDetailAdapter = new RefundDetailAdapter(R.layout.module_shopping_item_refund_detail, new ArrayList<>());
        rvRefundDetailList.setAdapter(refundDetailAdapter);
        rvRefundDetailList.addItemDecoration(decoration);

        View headView = LayoutInflater.from(mActivity).inflate(R.layout.refund_detail_goods_head, (ViewGroup) rvRefundGoodsList.getParent(), false);
        rvRefundGoodsList.setLayoutManager(new LinearLayoutManager(this));
        refundDetailGoodsAdapter = new RefundDetailGoodsAdapter(R.layout.module_shopping_item_refund_detail_goods, new ArrayList<>());
        refundDetailGoodsAdapter.addHeaderView(headView, 0);
        rvRefundGoodsList.setAdapter(refundDetailGoodsAdapter);
        getRefundDetail();
    }


    @Override
    protected void addListener() {
        tvBack.setOnClickListener(v -> finish());
    }

    private void getRefundDetail() {
        new RefundDetailTask(this, o -> {
            List<RefundDetailBean> detailBeans = (List<RefundDetailBean>) o;
            if (!Preconditions.isNullOrEmpty(detailBeans)) {
                refundDetailAdapter.setNewData(detailBeans);
                refundDetailGoodsAdapter.setNewData(detailBeans.get(0).getOrderInfo().getOrderItemList());
            }
        }).execute(orderDetailBean.getOrderId());
    }


    /**
     * 当前进度显示为卖家受理
     */
    private void showIndicatorTwo() {
        llRefundIndicatorOne.setBackgroundColor(Color.parseColor("#EEEEEE"));
        tvRefundIndicatorTwo.setBackgroundResource(R.mipmap.refund_schedule_hui);
        tvRefundIndicatorTwo.setTextColor(Color.parseColor("#dd2727"));
    }

    /**
     * 当前进度显示为完成退款
     */
    private void showIndicatorThree() {
        llRefundIndicatorTwo.setBackgroundColor(Color.parseColor("#EEEEEE"));
        tvRefundIndicatorThree.setBackgroundResource(R.mipmap.refund_schedule_hui);
        tvRefundIndicatorThree.setTextColor(Color.parseColor("#dd2727"));
    }

}
